package com.game.modular.game.playerMatch.service.base;

import com.game.modular.game.playerMatch.domain.PvpCityBuildings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PvpCityBuildBaseServiceImpl {
    private static final String cacheKey = "pvpMatch:%d:cityBuilding";
    private static final String cacheKey2 = "%d:%d";
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private HashOperations<String, String, PvpCityBuildings> hashOperations;


    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 通用查询
    public List<PvpCityBuildings> eqList(
            PvpCityBuildings select
    ) {
        if (select.getMatchId() == null) {
            throw new RuntimeException("matchId不能为空");
        }
        Long matchId = select.getMatchId();
        return listCache(matchId).stream().filter(item ->
                select.getCoreCityId() == null || Objects.equals(item.getCoreCityId(), select.getCoreCityId())
                        && select.getNo() == null || Objects.equals(item.getNo(), select.getNo())
                        && select.getPlayerId() == null || Objects.equals(item.getPlayerId(), select.getPlayerId())
                        && select.getCorePvpCityBuildingId() == null || Objects.equals(item.getCorePvpCityBuildingId(), select.getCorePvpCityBuildingId())
                        && select.getType() == null || Objects.equals(item.getType(), select.getType())
                        && select.getLevel() == null || Objects.equals(item.getLevel(), select.getLevel())
                        && select.getFinishTime() == null || Objects.equals(item.getStatus(), select.getStatus())
        ).collect(Collectors.toList());
    }

    public PvpCityBuildings allEqSelect(
            PvpCityBuildings select
    ) {
        return eqList(select).stream().findFirst().orElse(null);
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public Map<String, PvpCityBuildings> mapCache(
            @NotNull(message = "matchId不能为空") Long matchId
    ) {
        String key1 = String.format(cacheKey, matchId);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return hashOperations.entries(key1);
    }

    public List<PvpCityBuildings> listCache(
            @NotNull(message = "matchId不能为空") Long matchId
    ) {
        return new ArrayList<>(mapCache(matchId).values());
    }

    public void saveCache(
            PvpCityBuildings pvpCityBuildings
    ) {
        String key1 = String.format(cacheKey, pvpCityBuildings.getMatchId());
        String key2 = String.format(cacheKey2, pvpCityBuildings.getCoreCityId(), pvpCityBuildings.getNo());
        hashOperations.put(key1, key2, pvpCityBuildings);
    }
    public void removeCache(
            @NotNull(message = "matchId不能为空") Long matchId,
            @NotNull(message = "对局不能为空") Long coreCityId,
            @NotNull(message = "地块编号不能为空") Integer no
    ) {
        String key1 = String.format(cacheKey, matchId);
        String key2 = String.format(cacheKey2, coreCityId, no);
        hashOperations.delete(key1, key2);
    }
}
